package org.chickymate.client;

import java.util.List;

import org.chickymate.client.model.CommentDTO;
import org.chickymate.client.view.grid.CommentGrid;
import org.chickymate.client.view.grid.AbstractModelGrid;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

public class PopularCommentEntryPoint implements EntryPoint {
	
	private final CommentServiceAsync commentService = GWT.create(CommentService.class);
	private AbstractModelGrid grid;
	
	public void onModuleLoad() {
		grid = new CommentGrid();
		RootPanel.get(HtmlConstants.GWT_HOOK).add(grid);
		getPopularComments();
	}
	
	private void getPopularComments() {
		commentService.getPopularComments(new AsyncCallback<List<CommentDTO>>() {
			public void onFailure(Throwable caught) {
				throw new RuntimeException("Impossible to get most voted comments", caught.getCause());
			}
			
			public void onSuccess(List<CommentDTO> result) {
				grid.load(result);
			}
		});
	}
	
}
